package view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.text.DecimalFormat;

import javax.swing.JPanel;

import model.Bear;
import model.FieldStats;
import model.Fox;
import model.Rabbit;
import model.Wolf;

@SuppressWarnings("serial")
public class StatisticView extends JPanel{
	
	private FieldStats stats;
	
	private static final double DEGREES = 360.0;
	
	public StatisticView(FieldStats stats){
		super();
		this.stats = stats;
	}
	
	/**
	 * Draws the Pie Chart
	 */
	public void paintComponent(Graphics g){
		//Calculate precentages and degrees
		int yAxis = 480;
		DecimalFormat numberFormat = new DecimalFormat("#.0");
		double total = (double) stats.getAnimalCount();
		double foxesDegrees = Math.round(((double)stats.getClassCount(Fox.class)*DEGREES)/total);
		double rabbitsDegrees = Math.round(((double)stats.getClassCount(Rabbit.class)*DEGREES)/total);
		double bearsDegrees = Math.round(((double)stats.getClassCount(Bear.class)*DEGREES)/total);
		double wolvesDegrees = Math.round(((double)stats.getClassCount(Wolf.class)*DEGREES)/total);
		//Create two white areas
		g.setColor(new Color(230,230,230));
		g.fillRect(540, 280, 300, 200);
		g.fillRect(540, 0, 300, 200);
		
		int rabbits = (int) rabbitsDegrees;
		int foxes = (int) foxesDegrees;
		int bears = (int) bearsDegrees;
		int wolves = (int) wolvesDegrees;
		
		g.setColor(Color.RED);
		g.fillArc(605, 10, 180, 180, 0, rabbits);
		g.fillRect(560, yAxis - rabbits/2, 50, rabbits/2);
		g.fillRect(550, 10, 10, 10);
		
		g.setColor(Color.BLUE);
		g.fillArc(605, 10, 180, 180, rabbits, foxes);
		g.fillRect(630, yAxis - foxes/2, 50, foxes/2);
		g.fillRect(550, 30, 10, 10);
		
		g.setColor(Color.BLACK);
		g.fillArc(605, 10, 180, 180, rabbits+foxes, bears);
		g.fillRect(700, yAxis - bears/2, 50, bears/2);
		g.fillRect(550, 50, 10, 10);
		
		g.setColor(Color.MAGENTA);
		g.fillArc(605, 10, 180, 180, rabbits+foxes+bears, wolves);
		g.fillRect(770, yAxis - wolves/2, 50, wolves/2);
		g.fillRect(550, 70, 10, 10);
		
		g.setColor(Color.BLACK);
		Font font = new Font("Arial", Font.PLAIN, 9);
		g.setFont(font);
		g.drawString("Bears(" + numberFormat.format((stats.getClassCount(Bear.class)*100)/total)+"%)", 705, 500);
		g.drawString("Wolves("+ numberFormat.format((stats.getClassCount(Wolf.class)*100)/total)+"%)", 775, 500);
		g.drawString("Rabbits(" + numberFormat.format((stats.getClassCount(Rabbit.class)*100)/total)+"%)", 565, 500);
		g.drawString("Foxes("+ numberFormat.format((stats.getClassCount(Fox.class)*100)/total) +"%)", 635, 500);
		g.drawString(Integer.toString(stats.getClassCount(Fox.class)), 645, (yAxis - (foxes/2)-5));
		g.drawString(Integer.toString(stats.getClassCount(Rabbit.class)), 575, (yAxis - (rabbits/2)-5));
		g.drawString(Integer.toString(stats.getClassCount(Wolf.class)), 785, (yAxis - (wolves/2)-5));
		g.drawString(Integer.toString(stats.getClassCount(Bear.class)), 715, (yAxis - (bears/2)-5));
		g.drawString("Rabbits", 570, 18);
		g.drawString("Foxes", 570, 38);
		g.drawString("Bears", 570, 58);
		g.drawString("Wolves", 570, 78);

	}

}
